package com.mayisoftware.mayioa.common.impl.utils;

import com.alibaba.fastjson.JSONArray;
import com.mayisoftware.mayioa.common.impl.config.Global;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Excel模板工具类
 *
 */
public class ExcelExportUtil {
    public static String DEFAULT_DATE_PATTERN = "yyyy年MM月dd日";//默认日期格式
    public static int DEFAULT_COLOUMN_WIDTH = 17;
    public static String TEMP_XLSX = ".xlsx";

    public static String createExcelTemplate(String sheetName, Map<String, String> titleMap, JSONArray jsonArray)
    {
        FileOutputStream fout = null;
        SXSSFWorkbook workbook = null;
        String templateFolder = Global.getTemplatePath();
        pathIsExists(templateFolder);

        String templatePath = templateFolder + File.separator + sheetName + TEMP_XLSX;
        templatePath = templatePath.replace("\\","/");
        try {
            fout = new FileOutputStream(templatePath);

            // 创建工作簿
            workbook = new SXSSFWorkbook(1000);
            workbook.setCompressTempFiles(true);

            //表头样式
            CellStyle titleStyle = workbook.createCellStyle();
            titleStyle.setAlignment(HorizontalAlignment.CENTER);
            Font titleFont = workbook.createFont();
            titleFont.setFontHeightInPoints((short) 20);
            titleFont.setBold(true);
            titleStyle.setFont(titleFont);

            // 列头样式
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setBorderBottom(BorderStyle.THIN);
            headerStyle.setBorderLeft(BorderStyle.THIN);
            headerStyle.setBorderRight(BorderStyle.THIN);
            headerStyle.setBorderTop(BorderStyle.THIN);
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.getIndex());
            Font headerFont = workbook.createFont();
            headerFont.setFontHeightInPoints((short) 12);
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);

            // 单元格样式
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.getIndex());
            Font cellFont = workbook.createFont();
            cellFont.setBold(true);
            cellStyle.setFont(cellFont);

            // 创建一个sheet页
            SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(sheetName);

            //设置列宽
            int colWidth = 15;
            int minBytes = colWidth < DEFAULT_COLOUMN_WIDTH ? DEFAULT_COLOUMN_WIDTH : colWidth;//至少字节数
            int[] arrColWidth = new int[titleMap.size()];

            // 产生表格标题行,以及设置列宽
            String[] properties = new String[titleMap.size()];
            String[] headers = new String[titleMap.size()];
            int ii = 0;
            for (Iterator<String> iter = titleMap.keySet().iterator(); iter.hasNext(); ) {
                String fieldName = iter.next();

                properties[ii] = fieldName;
                headers[ii] = titleMap.get(fieldName);
                int bytes = fieldName.getBytes().length;
                arrColWidth[ii] =  bytes < minBytes ? minBytes : bytes;
                sheet.setColumnWidth(ii,arrColWidth[ii]*256);
                ii++;
            }

            SXSSFRow headerRow = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                headerRow.createCell(i).setCellValue(headers[i]);
                headerRow.getCell(i).setCellStyle(headerStyle);
            }

            workbook.write(fout);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                workbook.close();
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return templatePath;
    }

    private static void pathIsExists(String templateFolder) {
        File filePath = new File(templateFolder);
        if(!filePath.exists())
        {
            filePath.mkdirs();
        }
    }

//    public static void main(String[] args)
//    {
//        ExcelExportUtil.pathIsExists("E:/profile/template/plan");
//    }
}